<?php

namespace app\common\service;

use think\Exception;
use think\facade\Cache;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\console\Table;
use app\common\service\queue\Orders as OrdersQueue;
use app\common\service\queue\PushTask as PushTaskQueue;
use app\common\service\queue\UpdateUser as UpdateUserQueue;
use app\common\service\queue\Jobs as JobsQueue;
use app\common\service\queue\Resume as ResumeQueue;
use app\common\service\queue\Timing as TimingQueue;
use app\common\service\queue\Coupon as CouponQueue;
use app\common\service\queue\WorkWechat as WorkWechatQueue;
use app\common\service\queue\Api as ApiQueue;
use app\common\service\queue\Crm as CrmQueue;
use EasyTask\Task as EasyTask;
use XYCms\PushRelease;

class XYQueue extends Command
{
    /**
     * Version.
     *
     * @var string
     */
    const VERSION = '0.0.3';
    /**
     * [configure]
     * @return [type] [description]
     */
    protected  function configure()
    {
        //设置名称为task
        $this->setName('queue')
            //增加一个命令参数
            ->addArgument('action', Argument::OPTIONAL, "action", '')
            ->addArgument('data', Argument::OPTIONAL, "data", '');
    }
    protected function execute(Input $input, Output $output)
    {
        //获取输入参数
        $action = trim($input->getArgument('action'));
        $data   = trim($input->getArgument('data'));
        $usedPushTask = 1;
        $task = new EasyTask;

        $task->setDaemon(true);
        $task->setAutoRecover(true);
        $task->setPrefix('XYQueue');
        $task->setRunTimePath(runtime_path());
        /*订单*/
        $task->addFunc(function () {
            $OrdersQueue = new OrdersQueue;
            $OrdersQueue->expire_time();
            $OrdersQueue->notify_alipay_handle();
            $OrdersQueue->notify_wxpay_handle();
            // $OrdersQueue->scorepay_handle();
            // $OrdersQueue->rights_handle();
            $OrdersQueue->notify_applepay_handle();
            $OrdersQueue->notify_toutiao_handle();
            $OrdersQueue->notify_baidu_handle();
            $OrdersQueue->notify_kuaishou_handle();
            Cache::set('XYQueue.OrdersQueue', time());
        }, 'OrdersQueue', 5, 1);
        /*推送*/
        $task->addFunc(function () {
            $PushTaskQueue = new PushTaskQueue;
            $PushTaskQueue->push_handle();
            $PushTaskQueue->push_queue_handle();
            Cache::set('XYQueue.PushTaskQueue', time());
        }, 'PushTaskQueue', 3, 1);
        $task->addFunc(function () {
            try {
                $PushRelease = new PushRelease;
                $PushRelease->WechatPush();
                $PushRelease->SmsPush();
                $PushRelease->EmailPush();
                $PushRelease->AppPush();
            } catch (Exception $e) {
                echo json_encode(['line' => $e->getLine(), 'msg' => $e->getMessage(), 'file' => $e->getFile()], JSON_UNESCAPED_UNICODE);
            }
        }, 'PushReleaseQueue', 3, 1);
        /*用户*/
        $task->addFunc(function () {
            $UpdateUserQueue = new UpdateUserQueue;
            $UpdateUserQueue->update_user_handle();
            $UpdateUserQueue->company_vip_expiry_time_handle();
            $UpdateUserQueue->personal_vip_expiry_time_handle();
            $UpdateUserQueue->use_user_vip_rule_handle();
            $UpdateUserQueue->init_user_vip_handle();
            $UpdateUserQueue->auto_disable_handle();
            Cache::set('XYQueue.UpdateUserQueue', time());
        }, 'UpdateUserQueue', 5, 1);
        /*职位*/
        $task->addFunc(function () {
            $JobsQueue = new JobsQueue;
            $JobsQueue->auto_refresh_handle();
            $JobsQueue->auto_refresh_expire_handle();
            $JobsQueue->jobs_top_expire_handle();
            $JobsQueue->jobs_urgent_expire_handle();
            $JobsQueue->jobs_recommend_expire_handle();
            $JobsQueue->interview_handle();
            Cache::set('XYQueue.JobsQueue', time());
        }, 'JobsQueue', 5, 1);
        /*简历*/
        $task->addFunc(function () {
            $ResumeQueue = new ResumeQueue;
            $ResumeQueue->resume_auto_refresh_handle();
            $ResumeQueue->resume_auto_refresh_expire_handle();
            $ResumeQueue->resume_top_expire_handle();
            $ResumeQueue->resume_recommend_expire_handle();
            Cache::set('XYQueue.ResumeQueue', time());
        }, 'ResumeQueue', 5, 1);
        /*优惠券*/
        $task->addFunc(function () {
            $CouponQueue = new CouponQueue;
            $CouponQueue->auto_enable_handle();
            $CouponQueue->auto_disable_null_uid_handle();
            $CouponQueue->auto_disable_notnull_uid_handle();
            if (class_exists('app\common\service\queue\Crm')) {
                $CrmQueue = new CrmQueue;
                $CrmQueue->auto_cancel_adviser_handle();
            }
            Cache::set('XYQueue.CouponQueue', time());
        }, 'CouponQueue', 5, 1);
        /*定时任务*/
        $task->addFunc(function () {
            $TimingQueue = new TimingQueue;
            $TimingQueue->subscribe_handle();
            $TimingQueue->release_handle();
            Cache::set('XYQueue.TimingQueue', time());
        }, 'TimingQueue', 1, 1);
        /*删除日志*/
        $task->addFunc(function () {
            $ApiQueue = new ApiQueue;
            $ApiQueue->delete_api_log_handle();
            $ApiQueue->delete_admin_log_handle();
            $UpdateUserQueue = new UpdateUserQueue;
            $UpdateUserQueue->delete_user_vip_rule_handle();
            $CouponQueue = new CouponQueue;
            $CouponQueue->expire_del_handle();
            Cache::set('XYQueue.ApiQueue', time());
        }, 'ApiQueue', 600, 1);

        if (class_exists('app\common\service\queue\WorkWechat')) {
            $task->addFunc(function () {
                $workWechat = new WorkWechatQueue;
                $workWechat->add_half_external_contact();
                $workWechat->del_follow_user();
            }, 'WorkQueue', 1, 1);

            // 下载企微头像
            $task->addFunc(function () {
                $workWechatQueue = new WorkWechatQueue();
                // 员工
                $workWechatQueue->downloadEmployeesAvatar();
                // 外部联系人
                $workWechatQueue->downloadExternalAvatar();
            }, 'WorkWechatDownloadQueue', 15, 1);
        }

        if (class_exists('XYCms\MpTemplatePush')) {
            $task->addFunc(function (){
                $workWechat=new MpTemplatePush;
                $workWechat->pushWechatIndependent();
            },'MpTemplatePushQueue',1,1);
        }



        switch ($action) {
            case 'start':
                $output->writeln("正在启动XYCMS队列服务...");
                $output->writeln("XYQueue version:" . static::VERSION);
                $task->start();
                break;
            case 'status':
                $output->writeln("XYCMS队列服务状态");
                $output->writeln("XYQueue version:" . static::VERSION);
                $task->status();
                break;
            case 'stop':
                $output->writeln("正在停止XYCMS队列服务...");
                $task->stop(($data == 'f'));
                break;
            default:
                if (!in_array($action, ['?']))
                    $output->writeln("<error>不存在命令:{$action}</error>");
                $output->writeln("XYQueue version:" . static::VERSION);
                $Table = new Table();
                $Table->setStyle('borderless');
                $Table->setHeader(['Command', 'Description']);
                $Table->addRow(['start', '启动']);
                $Table->addRow(['status', '查看队列运行状态']);
                $Table->addRow(['stop', '停止队列']);
                $Table->addRow(['stop f', '强制停止队列']);
                $output->writeln($Table->render());
                break;
        }
    }
}
